﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using PaymentHelper.Methods.NganLuong.Models;
using PaymentHelper.Core.MVCUtils;

namespace PaymentHelper.Methods.NganLuong.Controllers
{
    public class NganLuongController : PaymentHelperController
    {
        protected override string SystemName
        {
            get { return "PaymentHelper.Methods.NganLuong"; }
        }

        [ChildActionOnly]
        public ActionResult Configure()
        {
            var settings = PaymentManager.Instance.LoadSettings(SystemName);

            var configModel = new ConfigModel();
            configModel.InitModel(settings);

            return View("PaymentHelper.Methods.NganLuong.Views.NganLuong.Configure", configModel);
        }

        [HttpPost]
        [ChildActionOnly]
        public ActionResult Configure(ConfigModel model)
        {
            if (!ModelState.IsValid)
                return View("PaymentHelper.Methods.NganLuong.Views.NganLuong.Configure", model);

            var listAtrr = model.ToListAttribute();
            bool isSuccess = PaymentManager.Instance.SaveSettings(SystemName,listAtrr);

            if (isSuccess)
                ViewBag.PaypalStandardMessage = "Changes are saved successfully !";
            else
                ViewBag.PaypalStandardMessage = "Changes are saved unsuccessfully !";

            return View("PaymentHelper.Methods.NganLuong.Views.NganLuong.Configure", model);
        }

        [ChildActionOnly]
        public ActionResult PaymentInfo()
        {
            var method = PaymentManager.Instance.LoadPaymentMethodBySystemName(SystemName);
            ViewBag.Resources = method.Descriptor.GetDefaultLanguage();

            return PartialView("PaymentHelper.Methods.NganLuong.Views.NganLuong.PaymentInfo");
        }

        // transaction_info=Demo&order_code=-8588626343462799508&price=85400&payment_id=8735&payment_type=1&error_text=&secure_code=cf4375195b2ee056035006881061ff9a
        public ActionResult Complete(string transaction_info, string order_code, string price, string payment_id, int payment_type, string error_text, string secure_code)
        {
            var model = new CompletedPaymentModel() { 
                ErrorText = error_text,
                OrderCode = order_code,
                PaymentType = payment_type,
                PaymerId = payment_id,
                Price = price,
                SecureCode = secure_code,
                TransactionInfo = transaction_info
            };

            var method = PaymentManager.Instance.LoadPaymentMethodBySystemName(SystemName) as NganLuongProcessor;
            
            var result = new ProcessPaymentResult();
            if (method.VerifyPaymentUrl(transaction_info,order_code,price,payment_id,payment_type.ToString(),error_text,secure_code))
            {
                result.Success = true;
                result.PaymentStatus = Core.RelatedObject.PaymentStatus.Paid;
            }
            else
            {
                result.Success = false;
                result.PaymentStatus = Core.RelatedObject.PaymentStatus.Pending;
            }

            PaymentManager.Instance.UpdateCurrentPaymentState(result);
            ViewBag.Resources = method.Descriptor.GetDefaultLanguage();

            var state = PaymentManager.Instance.GetCurrentPaymentState();
            ViewBag.ProcessResultUrl = state.UrlProcessResult;

            return View(SystemName + ".Views.NganLuong.Complete",model);
        }

        public override IList<string> ValidatePaymentInfo(FormCollection frmData)
        {
            return null;
        }
    }
}
